package 代码记录.校招笔试真题.笔试真题_2023.极致游戏;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author zx
 * @create 2023-02-23 21:51
 * 用栈实现队列
 * 37.5%
 */
public class Test2 {
    class MyQueue {
        Deque<Integer> inStack;//进栈
        Deque<Integer> outStack;//出栈
        public MyQueue() {
            this.inStack = new ArrayDeque<>();
            this.outStack = new ArrayDeque<>();
        }

        public void push(int x) {
            inStack.push(x);
        }

        public int pop() {
            //只要outStack为空,就将inStack中所有元素push到outStack中
            if(outStack.isEmpty()){
                while(!inStack.isEmpty()){
                    outStack.push(inStack.pop());
                }
            }
            return outStack.pop();
        }

        public int peek() {
            if(outStack.isEmpty()){
                while(!inStack.isEmpty()){
                    outStack.push(inStack.pop());
                }
            }
            return outStack.peek();
        }

        public boolean empty() {
            return inStack.isEmpty() && outStack.isEmpty();
        }
    }
}
